﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using Datalayer;
using Common;
using System.Data;

namespace Portal_KonTum
{
    public partial class Default : System.Web.UI.Page
    {
        IQueryMgr iq;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["lang"] = "vi";   
                BinData();
                
            }
        }
        void BinData()
        {
            try
            {
                List<Category> ex = new List<Category>();
                iq = QueryMgr.GetDbInstance();
                //tin tức sự kiện ID ="12a5f42d-ea42-4678-bc14-58bde3c40096"
                ex = iq.Ca_GetByCateParentID(MethodCommon.Get_Guid("12a5f42d-ea42-4678-bc14-58bde3c40096"));
                DataList1.DataSource = createTable(ex);
                DataList1.DataBind();

                List<New> ne = new List<New>();
                ne = iq.News_GetNewsHot5();
                if (Session["lang"] == "vi")
                {
                    DataList2.DataSource = ne;
                    DataList2.DataBind();
                    DataList2.Visible = true;
                    DataList_en.Visible = false;
                }
                else
                {
                    DataList_en.DataSource = ne;
                    DataList_en.DataBind();
                    DataList_en.Visible = true;
                    DataList2.Visible = false;
                }
            }
            catch (Exception ex)
            {
                Alert.Show("Lỗi :" + ex.ToString());
            }

        }

        DataTable createTable(List<Category> ex)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("CateID", typeof(Guid));
	        dt.Columns.Add("CategoryName", typeof(string));
            dt.Columns.Add("Content", typeof(string));
            
            IQueryMgr iq = QueryMgr.GetDbInstance();
            foreach (Category obj in ex)
            {
                List<New> listnew= new List<New>();
                listnew = iq.News_GetNewByCatID(obj.CateID);
                if (listnew.Count > 0)
                {
                    if (Session["lang"] == "vi")
                    {
                        string s = "<h1><a href='ViewNews.aspx?NewID=" + listnew[0].NewID + "'>" + listnew[0].NewTitle + "</a></h1>" +
                        "<i align='right'>" + listnew[0].CreateDate.ToShortDateString() + "</i><br />" +
                        "<img height='120px' src='\\Admin\\Uploads\\Images\\" + listnew[0].Image + "' width='160px' />" +
                        "<div class='news_content'>"
                            + listnew[0].NewIntro + "<div align='right' style='margin:5px; text-align:right'><a href='ViewNews.aspx?NewID=" + listnew[0].NewID + "' style='color:#F00'>Xem Tiếp</a></div>" +
                        "</div><br/><b style='color:#CCCC'>Các tin khác:</b>";

                        if (listnew.Count < 5)
                        {
                            for (int i = 1; i < listnew.Count; i++)
                            {
                                s = s + "<li class='sub'><a href='ViewNews.aspx?NewID=" + listnew[i].NewID + "'>" + listnew[i].NewTitle + "</a></li>";
                            }
                        }
                        else
                        {
                            for (int i = 1; i < 5; i++)
                            {
                                s = s + "<li class='sub'><a href='ViewNews.aspx?NewID=" + listnew[i].NewID + "'>" + listnew[i].NewTitle + "</a></li>";
                            }
                        }

                        dt.Rows.Add(obj.CateID, obj.CategoryName, s);
                    }
                    else
                    {
                        string s = "<h1><a href='ViewNews.aspx?NewID=" + listnew[0].NewID + "'>" + listnew[0].NewTitle_en + "</a></h1>" +
                        "<i align='right'>" + listnew[0].CreateDate.ToShortDateString() + "</i><br />" +
                        "<img height='120px' src='\\Admin\\Uploads\\Images\\" + listnew[0].Image + "' width='160px' />" +
                        "<div class='news_content'>"
                            + listnew[0].NewIntro_en + "<div align='right' style='margin:5px; text-align:right'><a href='ViewNews.aspx?NewID=" + listnew[0].NewID + "' style='color:#F00'>read more</a></div>" +
                        "</div><br/><b style='color:#CCCC'>Other:</b>";

                        if (listnew.Count < 5)
                        {
                            for (int i = 1; i < listnew.Count; i++)
                            {
                                s = s + "<li class='sub'><a href='ViewNews.aspx?NewID=" + listnew[i].NewID + "'>" + listnew[i].NewTitle_en + "</a></li>";
                            }
                        }
                        else
                        {
                            for (int i = 1; i < 5; i++)
                            {
                                s = s + "<li class='sub'><a href='ViewNews.aspx?NewID=" + listnew[i].NewID + "'>" + listnew[i].NewTitle_en + "</a></li>";
                            }
                        }

                        dt.Rows.Add(obj.CateID, obj.CategoryName, s);
                    }
                }
            }
            return dt;
        }
    }
}